package com.panshi.service.impl;

import com.panshi.dao.ISearchDao;
import com.panshi.service.ISearchService;
import com.panshi.vo.qa.Question;
import com.panshi.vo.search.Paging;
import com.panshi.vo.search.ArticleVo;
import com.panshi.vo.search.SearchVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SearchServiceImpl implements ISearchService {
    @Autowired
    private ISearchDao searchDao;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public void addQaSearch(Question question) {
        searchDao.addQaSearch(question);
        redisTemplate.delete("hasbei:search");
        redisTemplate.delete("hasbei:total");
    }

    @Override
    public void addSearch(SearchVo searchVo) {
        searchDao.addSearch(searchVo);
        redisTemplate.delete("hasbei:search");
        redisTemplate.delete("hasbei:total");
    }

    @Override
    public void updateQaSearch(Question question) {
        searchDao.updateQaSearch(question);
        redisTemplate.delete("hasbei:search");
        redisTemplate.delete("hasbei:total");
    }

    @Override
    public void updateSearch(SearchVo searchVo) {
        searchDao.updateSearch(searchVo);
        redisTemplate.delete("hasbei:search");
        redisTemplate.delete("hasbei:total");
    }

    @Override
    public Paging<List<SearchVo>> search(Paging paging){
        log.info(paging.toString()+"最后----------------------------");
        List<SearchVo> search =  null;
        Paging<List<SearchVo>> pa = new Paging<>();
        if (redisTemplate.hasKey("hasbei:search:"+paging.getRows()+paging.getCurrentPage())){
            search = (List<SearchVo>) redisTemplate.opsForValue().get("hasbei:search:"+paging.getRows()+paging.getCurrentPage());
        }else {
            log.info(paging.toString()+"========");
            search = searchDao.search(paging);
            redisTemplate.opsForValue().set("hasbei:search:"+paging.getRows()+paging.getCurrentPage(),search);
        }
        if (redisTemplate.hasKey("hasbei:total:"+paging.getRows())){
            pa.setTotal((int) redisTemplate.opsForValue().get("hasbei:total:"+paging.getRows()));
        }else {
            int total = searchDao.total((String) paging.getRows());
            pa.setTotal(total);
            log.info(pa.getTotal()+"======="+paging.getRows());
            redisTemplate.opsForValue().set("hasbei:total:"+paging.getRows(), total);
        }
        pa.setRows((List<SearchVo>) search);
        pa.setCurrentPage(paging.getCurrentPage());
        return pa;
    }

    @Override
    public List<ArticleVo> findSou() {
        List<ArticleVo> list = null;
        if (redisTemplate.hasKey("hasbei:findSou:zx")){
            list = (List<ArticleVo>) redisTemplate.opsForValue().get("hasbei:findSou:zx");
        }else {
           list = searchDao.findSou();
            redisTemplate.opsForValue().set("hasbei:findSou:zx", list,2, TimeUnit.DAYS);
        }
        return list;
    }

}
